var idtext = 0;
var nametext = "";
var isLeaf = true;
var preNode;
var tree = eval("tree");
var checkedNodeIdList = document.getElementById("checkedNodeIdList");
checkedNodeIdList.value = "";
var nodeidArr = [];

var dragsuccess = false;//add by yqx 用来判断拖拽是否成功的标志

tree.on("click", function(node) {

	idtext = node.id;

	nametext = node.text;

	var id = idtext;
	var name = nametext;
	var text = nametext;

	if (onNodeClick != "") {
		eval(onNodeClick);
	}
});

tree.on("checkchange", function(node, checked) {

	var id = node.id;
	var name = node.text;
	var text = name;

	checkedNodeIdList.value = tree.getChecked("id").toString();
	if (onCheckChange != "") {
		eval(onCheckChange);
	}
}, tree);

tree.on("nodedragover", function(e) {
	if (leafEnableDrop) {
		var n = e.target;

		if (n.leaf) {

			if (preNode) {

				if (preNode.id != n.id) {

					preNode.leaf = isLeaf;
				}
			}

			preNode = n;

			isLeaf = n.leaf;

			n.leaf = false;
		}
		return true;
	}
});

tree.on("nodedrop", function(e) {
	if (e.target.firstChild) {
		e.target.leaf = false;
	}
	
	//add by yqx 主要是用来在拖拽后进行高亮显示
	if (enddragScript != "") {
		eval(enddragScript);//主要执行其它的客户端代码，一般进行状态处理
	}
	if(dragsuccess){
		e.dropNode.select();//主要用来在节点被拖拽后进行高亮显示的
		tree.fireEvent('click', e.dropNode, null);
		dragsuccess = false;
	}
	//add by yqx
});

tree.on("beforenodedrop", function(e) {
	
	//add by yqx 用来在拖拽之前进行状态判断的
	if (beforeDDScript != "") {
		if(!eval(beforeDDScript)){
			return false;
		}
	}	
	//add by yqx
	
	nodeidArr = [];
	if (e.data.nodes) {
		for (var i = 0; i < e.data.nodes.length; i++) {
			nodeidArr[i] = e.data.nodes[i].id;
		}
	}
	else {
		nodeidArr[0] = e.dropNode.id;
		if(e.dropNode.parentNode == e.target){
			return false;
		}
	}
	tree.beforeNodeDropGoConfirmText = "您将节点“" + e.dropNode.text + "”拖到了节点“" + e.target.text + "”的"
			+ getdropstring(e.point) + ",确定修改吗？";
	return true;
}, tree);

//add by yqx
tree.on("beforenodedropgoconfirmok",function(e){
	return true;
});

tree.on("afternodedropgoconfirmok",function(e){
	var re = Ext.Ajax.request({
		url : beforeDDAction,
		callback : function(response, success) {
		},
		async : false,
		scope : this,
		params : {
			// 放下节点的方式，“append”：放在目标节点里；“above”：放在节点上方；“below”：放在下方
			point : e.point,
			// 目标节点的id
			target : e.target.id,
			// 拖拽节点的id
			node : nodeidArr
		}
	});
	// alert("您将节点" + e.dropNode.text + "拖到了节点" + e.target.text
	// +
	// "的" +
	// getdropstring(e.point));

	var result = re.conn.responseText.split(",")[0];
	var error = re.conn.responseText.split(",")[1];
	if (true == eval(result)){
		dragsuccess = true;//add by yqx
		return true;
	}else {
		Ext.Msg.alert("错误", error);
		return false;
	}
})
//add by yqx

//add by yqx 主要是用来在拖拽后进行高亮显示
//tree.on("enddrag", function(t,n,e) {
//	if (enddragScript != "") {
//		eval(enddragScript);//主要执行其它的客户端代码，一般进行状态处理
//	}
//	if(dragsuccess){
//		n.select();//主要用来在节点被拖拽后进行高亮显示的
//		tree.fireEvent('click', n, null);
//		dragsuccess = false;
//	}
//})
//add by yqx

function ddConfirm(btn) {

}

function getdropstring(point) {
	if (point == "append")
		return "里面";
	else if (point == "above")
		return "上面";
	else if (point == "below")
		return "下面";
}

function addChildNode(nodetext, nodeid, isSon, targetnodeid, nodeicon, leafflag, needcheck) {
	var theNode = tree.getNodeById(idtext);

	if (targetnodeid) {
		theNode = tree.getNodeById(targetnodeid);
	}

	if (!isSon) {
		if (theNode.parentNode)
			theNode = theNode.parentNode;
	}

	var newNode;
	if((needcheck != undefined)
		&&(needcheck != null)
		&&!needcheck){
		newNode = new Tree.AsyncTreeNode({
			text : nodetext,
			//------add by yxr-----------
			icon:nodeicon,
			//------add by yxr end----------
			id : nodeid,
			leaf : true
		});
	}else{
		newNode = new Tree.AsyncTreeNode({
			text : nodetext,
			id : nodeid,
			//------add by yxr-----------
			icon:nodeicon,
			//------add by yxr end----------
			checked : false,
			leaf : true
		});
	}
	
//	if ((nodeicon != null) && (nodeicon != undefined)) {
//		newNode.icon = nodeicon;
//	}
	
	if ((leafflag != null) && (leafflag != undefined)) {
		newNode.leaf = leafflag;
	}	
	
	if(!theNode.isExpanded()){
		theNode.on("expand", addDealWhenExpand);
	}
	
	var firstc = theNode.firstChild;
		
	if (theNode.id == tree.root.id && null == firstc) {
		tree.loader.dataUrl = treeLoader;
		tree.loader.clearOnLoad = true;
		tree.loader.load(root, function() {			
			root.expand(false, true);
			if(root.isExpanded()){
				root.childNodes[root.childNodes.length - 1].select();
			}
		});		
		return;
	}
	if (theNode.leaf) {
		theNode.leaf = false;
		theNode.appendChild(newNode);
		theNode.expand(false, true);
		tree.fireEvent('click', newNode, null);
		// newNode.click();
	} else {
		var firstc = theNode.firstChild;
		theNode.expand(false, true);
		
		if (firstc) {
			theNode.appendChild(newNode);
		}else{
			if(theNode.isExpanded()){
				theNode.appendChild(newNode);
			}else{
				theNode.expand(false, true);
			}
		}
		//alert(newNode.parentNode);
		tree.fireEvent('click', newNode, null);		
	}
}

function addDealWhenExpand(e){
	//alert(e.childNodes.length);
	if(e.childNodes.length!=0)
	   e.childNodes[e.childNodes.length - 1].select();
	e.un("expand", addDealWhenExpand);
}

function delChechedNode() {
	var s = tree.getChecked("id");
	var i = 0;
	for (; i < s.length; i++) {
		var delNode = tree.getNodeById(s[i]);
		if (delNode) {
			delNode.remove();
		}
	}
}

function updateNode(nodetext, nodeid) {
	var theNode = tree.getNodeById(idtext);
	if (nodeid)
		theNode = tree.getNodeById(nodeid);

	theNode.setText(nodetext);
	nametext = theNode.text;
	idtext = theNode.id;
}

function refreshChildNode(nodeid) {
	var theNode = tree.getNodeById(idtext);
	if (nodeid)
		theNode = tree.getNodeById(nodeid);

	tree.loader.clearOnLoad = true;
	tree.loader.load(theNode,function(){theNode.expand(false,true);});
}

function replaceSelectNode(newNode,oldNode) {
	if( null == oldNode)
	   oldNode = tree.getNodeById(idtext);
	if(newNode.id == idtext){
	   oldNode.id = -7789743;
	}
	oldNode.parentNode.replaceChild(newNode,oldNode);
	tree.fireEvent('click', newNode, null);	
}